﻿//
// main entry
define(function (require) {
    // 检测没有登录就跳转
    require('ezdesk/utility/test-login');
    require('ezdesk/utility/window-drag');
    require('ezdesk/utility/window-resize');

    var iContentMap = {
        "feed": "i-feed",
        "app": "i-app",
        "workitem": "i-workitem",
        "project": "i-project"
    };

    // 处理 main 窗体的改变大小的事情
    $('#main').resizable({
        minHeight: 652,
        minWidth: 354,
        resize: function (event, ui) {
            setInterval(function () {
                //navBar and conten width
                $('#i-nav-bar').width($('#main').width());
                $('.i-content').width($('#main').width());
                //conten height
                var content = $(".i-content").contents().find(".content");
                content.height(
                    $('#main').height() - $('#i-nav-bar').height() - $('#title-bar').height()
                );
                $(".i-content").height(content.height() - 5);
                //messageAction
                $('#action-message').width($('#main').width() - 12); //12 is padding
            }, 50);
        }
    });


    $(function () {
        var service = require('ezdesk/utility/service');
        var baseUrl = service.ClientBaseUrl_Sync();

        service.CurrentUser(function (e) {
            $("#user-name").html(e.Name);
            $("#user-dept").html("(" + e.Dept.DeptName + ")");
            $("#user-pos").html(e.Position.Description);
            var headIconUrl = 'http://{baseUrl}/core/headicon/{userId}'
                .replace('{baseUrl}', baseUrl)
                .replace('{userId}', e.Id);

            $('#img-head').prop('src', headIconUrl);
        });
        service.OrganizationInfo(function (e) {
            $(".page-title").html(e.Title);
        });
        setMainWindowLocation();


        $('.func-min,.func-close').click(function () {
            $.postMessage('min', '*', parent);
        });

        $.receiveMessage(function (e) {
            var paramKeyRegex = /\w*(?=\:)/g;
            var key = paramKeyRegex.exec(e.data);
            key = key ? key[0] : e.data;

            if (key == 'message')
                showActionMesaage(e.data.substring(key.length + 1));
            if (key == 'action')
                doAction(e.data.substring(key.length + 1));
            if (key == "tab") {
                switchTab(e.data.substring(key.length + 1));
            }
            else
                $.postMessage(e.data, '*', parent);

        });

        function doAction(actionUrl) {
            $.postMessage(actionUrl, '*', $('#i-action-request')[0].contentWindow);
        }

        function switchTab(tabItem) {
            $('.currentTabContent').removeClass('currentTabContent').hide();
            $('#' + iContentMap[tabItem]).addClass('currentTabContent').fadeIn();
        }

        function showActionMesaage(data) {
            $('#txt-message').html(data);

            $('#action-message').css({
                "margin-top": 80 + 28,
                "width": $('#main').width() - 12
            });

            $('#action-message').fadeIn();
            setTimeout(function () {
                $('#action-message').fadeOut();
            }, 3000);
        };

        function setMainWindowLocation() {
            var windowWidth = document.documentElement.offsetWidth;
            var windowHeight = document.documentElement.offsetHeight;
            var mainHeight = $("#main").height();
            var mainWidth = $("#main").width();

            var mainTop = windowHeight / 2 - mainHeight / 2 - 100;
            mainTop = mainTop < 50 ? 50 : mainTop;

            var mainLeft = windowWidth - mainWidth - 35;
            mainLeft = mainLeft < 10 ? 10 : mainLeft;
            //centering
            $('#main').css({
                "position": "absolute",
                "top": mainTop,
                "left": mainLeft
            }).show();
        }
    })
});